package com.qf.weektest.weektest.service.impl;


import cn.hutool.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qf.weektest.util.GetCookieByTel;
import com.qf.weektest.util.JWTUtil;
import com.qf.weektest.vo.Constants;
import com.qf.weektest.vo.ResultVo;
import com.qf.weektest.weektest.entity.Tel;
import com.qf.weektest.weektest.mapper.TelMapper;
import com.qf.weektest.weektest.service.ITelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.time.Year;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ljc
 * @since 2023-02-24
 */
@Service
public class TelServiceImpl extends ServiceImpl<TelMapper, Tel> implements ITelService {

    /**
     * cookie判定在一分钟之内只能允许获得一个token
     * 删除原有cookie添加更改后的cookie
     * @param request
     * @param response
     * @param telephone
     * @return
     * @throws UnsupportedEncodingException
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo getToken(HttpServletRequest request, HttpServletResponse response, String telephone) throws UnsupportedEncodingException {
        String token= null;
        Cookie[] cookies = request.getCookies();


        if (cookies==null){
            token = GetCookieByTel.newCookieByTel(telephone,response);

            save(new Tel(telephone,token,0));

            return ResultVo.ok(token);

        }

        Map<String, Cookie> collect = Arrays.stream(cookies).collect(Collectors.toList()).stream().collect(Collectors.toMap(cookie -> cookie.getName(), cookie -> cookie));

        Cookie cookie = collect.get(telephone);

       if (cookie==null){

            token = GetCookieByTel.newCookieByTel(telephone,response);

            save(new Tel(telephone,token,0));

            return ResultVo.ok(token);
        }

        token = cookie.getValue();

        boolean validate = JWT.of(token).setKey(Constants.SECRET.getBytes("utf-8"))
                .validate(0);

            if(!validate) {
                token = JWTUtil.getToken(telephone);

                cookie.setPath("/");

                cookie.setMaxAge(0);

                response.addCookie(cookie);

                Cookie cookie2 = new Cookie(telephone, token);

                response.addCookie(cookie2);


                update(new Tel(telephone, token,0), new UpdateWrapper<Tel>().eq("telephone", telephone));

                return ResultVo.ok(token);

            }

            return ResultVo.ok();

    }

    
}
